package com.chukun.gateway.core.netty.server;

import com.chukun.gateway.core.processor.NettyProcessor;
import com.chukun.gateway.core.request.HttpRequestWrapper;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.http.FullHttpRequest;

/**
 * @author chukun
 * @version 1.0.0
 * @description netty服务端处理器
 * @createTime 2023/11/25 21:02
 */
public class NettyHttpServerHandler extends ChannelInboundHandlerAdapter {

    private final NettyProcessor nettyProcessor;

    public NettyHttpServerHandler(NettyProcessor nettyProcessor) {
        this.nettyProcessor = nettyProcessor;
    }


    @Override
    public void channelRead(ChannelHandlerContext ctx, Object message) throws Exception {
        if (message instanceof FullHttpRequest) {
            FullHttpRequest request = (FullHttpRequest) message;
            HttpRequestWrapper httpRequestWrapper = new HttpRequestWrapper();
            httpRequestWrapper.setCtx(ctx);
            httpRequestWrapper.setRequest(request);
            // 请求处理
            nettyProcessor.process(httpRequestWrapper);
        }
    }
}
